package mr

//
// RPC definitions.
//
// remember to capitalize all names.
//

import (
	"fmt"
	"os"
	"strconv"
	"time"
)

const TIMEOUT = 10 * time.Second

type TaskType uint8

const (
	Map TaskType = iota
	Reduce
	Exit
)

type TaskAssignment struct {
	Id       int
	Type     TaskType
	DataPath []string
	NReduce  int
}

func (ta *TaskAssignment) Uid() string {
	return fmt.Sprintf("%d-%d", ta.Type, ta.Id)
}

// it returns failed task result
func (ta *TaskAssignment) ToTaskResult() TaskResult {
	return TaskResult{Id: ta.Id, Type: ta.Type, ResultPath: nil, DataPath: ta.DataPath}
}

type TaskResult struct {
	Id         int
	Type       TaskType
	ResultPath []string
	DataPath   []string
}

func (tr *TaskResult) Uid() string {
	return fmt.Sprintf("%d-%d", tr.Type, tr.Id)
}

//
// example to show how to declare the arguments
// and reply for an RPC.
//

type ExampleArgs struct {
	X int
}

type ExampleReply struct {
	Y int
}

// Add your RPC definitions here.

// Cook up a unique-ish UNIX-domain socket name
// in /var/tmp, for the coordinator.
// Can't use the current directory since
// Athena AFS doesn't support UNIX-domain sockets.
func coordinatorSock() string {
	s := "/var/tmp/5840-mr-"
	s += strconv.Itoa(os.Getuid())
	return s
}
